package com.sxz.sbs.widget;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.module.home.HomeActivity;
import com.sxz.sbs.module.message.MyMessageActivity;
import com.sxz.sbs.other.TopRightMenuAdapter;
import com.sxz.sbs.other.TopRightMenuBean;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理title
 * @author tian
 */

public class CommonTitle {
    private Object titleName;
    private boolean isShowBack = true;
    private boolean isShowRightText = false;
    private boolean isShowMore = true;
    private boolean isShowLeftImg = false;
    //是否显示右侧备用图片
    private boolean isShowRightReadyImg = false;
    /**
     * 左侧图片资源ID
     */
    private int leftReadyImgId;
    private ImageView ivBack;
    private TextView textView;
    private TextView rightText;
    private Activity activity;
    private ImageView more;
    private List<TopRightMenuBean> dataList = new ArrayList<>();
    private EasyPopup easyPopup;
    private View popupView;
    private OnMenuClickListener onMenuClickListener;
    private ImageView ivLeftImg;
    /**
     * 右侧备用 图片
     */
    private ImageView ivRightImageReady;
    /**
     * 右侧备用图片资源ID
     */
    private int rightReadyImgId;
    /**
     * 通常类标题格式
     * @param activity
     * @param titleName
     */
    public static void customTitleBar(final Activity activity, Object titleName) {
        ImageView ivBack = (ImageView) activity.findViewById(R.id.iv_title_back);
        ivBack.setOnClickListener
                (new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                             activity.finishAfterTransition();
                         } else {
                             activity.finish();
                         }
                     }
                 }
                );
        TextView textView = (TextView) activity.findViewById(R.id.tv_nav_title);
        if (titleName instanceof String) {
            textView.setText((String)titleName);
        } else {
            textView.setText((int)titleName);
        }
        ImageView more = (ImageView) activity.findViewById(R.id.iv_title_more);
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyPopup mQQPop = new EasyPopup(activity)
                        .setContentView(R.layout.layout_right_pop)
                        .setFocusAndOutsideEnable(true)
                        .createPopup();
                mQQPop.showAtAnchorView(v, VerticalGravity.BELOW, HorizontalGravity.LEFT, SizeUtils.dp2px(35), -30);
            }
        });
    }
    public void show () {
        ivBack.setVisibility(isShowBack?View.VISIBLE:View.GONE);
        rightText.setVisibility(isShowRightText?View.VISIBLE:View.GONE);
        more.setVisibility(isShowMore?View.VISIBLE:View.GONE);
        if (isShowLeftImg) {
            ivLeftImg.setVisibility(View.VISIBLE);
            ivLeftImg.setImageResource(leftReadyImgId);
        }
        if (isShowRightReadyImg) {
            ivRightImageReady.setVisibility(View.VISIBLE);
            ivRightImageReady.setImageResource(rightReadyImgId);
        }
        ivBack.setOnClickListener
                (new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                             activity.finishAfterTransition();
                         } else {
                             activity.finish();
                         }
                     }
                 }
                );
        if (titleName instanceof String) {
            textView.setText((String)titleName);
        } else {
            textView.setText((int)titleName);
        }
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.add(new TopRightMenuBean(R.drawable.ic_bubble,"我的消息"));
                dataList.add(new TopRightMenuBean(R.drawable.ic_house,"返回首页"));
                if (easyPopup == null) {
                    popupView = View.inflate(activity,R.layout.layout_right_pop,null);
                    easyPopup  = new EasyPopup(activity)
                            .setContentView(popupView)
                            .setFocusAndOutsideEnable(true)
                            .createPopup();
                    initPopView(popupView);
                }
                easyPopup.showAtAnchorView(v, VerticalGravity.BELOW, HorizontalGravity.LEFT, SizeUtils.dp2px(35), -30);
                easyPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        dataList.clear();
                    }
                });
            }
        });
    }

    private void initPopView(View popupView) {
        RecyclerView recyclerView = popupView.findViewById(R.id.popup_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerItemDecoration(activity,DividerItemDecoration.VERTICAL));
        TopRightMenuAdapter adapter = new TopRightMenuAdapter(dataList);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (dataList.get(position).getText().equals("我的消息")) {
                    Intent intent = new Intent(activity, MyMessageActivity.class);
                    activity.startActivity(intent);
                } else if (dataList.get(position).getText().equals("返回首页")) {
                    Intent intent = new Intent(activity, HomeActivity.class);
                    activity.startActivity(intent);
                } else {
                    if (onMenuClickListener != null) {

                        onMenuClickListener.onMenuItemClick(view,position);
                    }
                }
                easyPopup.dismiss();
            }
        });

    }

    private CommonTitle (Activity activity) {
        this.activity = activity;
        ivBack = activity.findViewById(R.id.iv_title_back);
        textView = activity.findViewById(R.id.tv_nav_title);
        more = activity.findViewById(R.id.iv_title_more);
        rightText = activity.findViewById(R.id.tv_title_right);
        ivLeftImg = activity.findViewById(R.id.iv_title_left_img);
        ivRightImageReady = activity.findViewById(R.id.iv_title_standby);
    }
    private CommonTitle (Fragment fragment) {

    }
    public static class Builder {
        CommonTitle commonTitle;
        public Builder(Activity activity) {
            commonTitle = new CommonTitle(activity);
        }
        public Builder(Fragment fragment) {
            commonTitle = new CommonTitle(fragment);
        }
        /**
         * 标题
         * @param title
         * @return
         */
        public Builder setTitle(@NonNull Object title) {
            commonTitle.titleName = title;
            return this;
        }
        /**
         * 是否显示返回键
         * @param isShowBack
         * @return
         */
        public Builder showBack(boolean isShowBack) {
            commonTitle.isShowBack = isShowBack;
            return this;
        }
        /**
         * 是否显示右侧文字
         * @param showRightText
         * @return
         */
        public Builder showRightText(boolean showRightText) {
            commonTitle.isShowRightText = showRightText;
            return this;
        }
        /**
         * 是否显示右侧更多...
         * @param showMore
         * @return
         */
        public Builder showMore(boolean showMore) {
            commonTitle.isShowMore = showMore;
            return this;
        }
        /**
         * 菜单点击监听
         * @param onMenuClickListener
         * @return
         */
        public Builder setOnMenuItemClick(OnMenuClickListener onMenuClickListener) {

            if (onMenuClickListener != null){
                commonTitle.onMenuClickListener = onMenuClickListener;
            }
            return this;
        }

        /**
         * 显示左侧备用图片 加资源id
         * @param drawableId 图片资源
         * @return
         */
        public Builder showLeftReadyImg(int drawableId) {
            commonTitle.isShowLeftImg = true;
            commonTitle.leftReadyImgId = drawableId;
            return this;
        }
        /**
         * 显示右侧备用图片 加资源id
         * @param drawableId 图片资源
         * @return
         */
        public Builder showRightReadyImg(int drawableId) {
            commonTitle.isShowRightReadyImg = true;
            commonTitle.rightReadyImgId = drawableId;
            return this;
        }
        /**
         * 添加数据源
         * @param data
         * @return
         */
        public Builder setMenuData(List<TopRightMenuBean> data) {
            commonTitle.dataList = data;
            return this;
        }

        /**
         * 创建
         * @return
         */
        public CommonTitle build() {
            return commonTitle;
        }
    }
    public interface OnMenuClickListener {
        void onMenuItemClick (View view,int position);
    }
}
